package com.ougy.reshop.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ougy.reshop.model.GoodsClass;

public class GoodsClassAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1058265789791170397L;
	private Logger logger=Logger.getLogger(getClass());
	
	private String result;
	private int parentId;
	
	public int getParentId() {
		return parentId;
	}
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}

	@Override
	public String execute() throws Exception {
		
		return "success";
	}
	public String children() throws Exception{
		DetachedCriteria detachedCriteria=DetachedCriteria.forClass(GoodsClass.class,"g");
		detachedCriteria.add(Restrictions.eq("g.parent.id",parentId));
		try{
			List<GoodsClass> list=sGoodsClass.findAllByCriteria(detachedCriteria);
			Gson gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
			result=gson.toJson(list,new TypeToken<List<GoodsClass>>(){}.getType());
			logger.debug(result);
		}catch(Exception e){
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
}